//
//  BYMineInfoView.m
//  OCDemo
//
//  Created by 朱慕之 on 2022/1/4.
//

#import "BYMineInfoView.h"

@interface BYMineInfoView ()

// 注册
@property (nonatomic, strong) UIButton *signin;
// 登录
@property (nonatomic, strong) UIButton *login;
// 头像
@property (nonatomic, strong) UIImageView *icon;
// 用户名
@property (nonatomic, strong) UILabel *acountName;
// 用户类型
@property (nonatomic, strong) UILabel *acountType;
// 背景图片
@property (strong, nonatomic) UIImageView *headBackImage;

@end


@implementation BYMineInfoView

#pragma mark - Event

- (void)signinEvent {
    if (self.signinBlock) {
        self.signinBlock();
    }
}

- (void)liginEvent {
    if (self.loginBlock) {
        self.loginBlock();
    }
}

#pragma mark - SetData
- (void)setData {
    if([AppConfig isLogin]) {
        
        self.signin.hidden = YES;
        self.login.hidden = YES;
        self.icon.hidden = NO;
        self.acountName.hidden = NO;
        self.acountType.hidden = NO;
        
        NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
        NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
        NSString *userName = [loginDict objectForKey:@"user_name"];
        NSString *userType = [loginDict objectForKey:@"user_type"];
        _acountName.text = userName;
        _acountType.text = userType;
    }else {
        self.signin.hidden = NO;
        self.login.hidden = NO;
        self.icon.hidden = YES;
        self.acountName.hidden = YES;
        self.acountType.hidden = YES;
    }
}

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.signin];
        [self addSubview:self.login];
        
        [self addSubview:self.icon];
        [self addSubview:self.acountName];
        [self addSubview:self.acountType];
    }
    return self;
}

- (void)layoutSubviews {
    
    [self.signin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(-kAutoWithSize(40));
        make.centerY.equalTo(self).offset(-kAutoWithSize(20));
        make.width.mas_equalTo(kAutoWithSize(60));
        make.height.mas_equalTo(kAutoWithSize(30));
    }];

    [self.login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(kAutoWithSize(40));
        make.centerY.equalTo(self).offset(-kAutoWithSize(20));
        make.width.mas_equalTo(kAutoWithSize(60));
        make.height.mas_equalTo(kAutoWithSize(30));
    }];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.size.mas_equalTo(CGSizeMake(60.0f, 60.0f));
    }];
    
    [self.acountName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.icon.mas_centerY).offset(-kAutoWithSize(12));
        make.left.equalTo(self.icon.mas_right).offset(kAutoWithSize(12));
        make.right.equalTo(self).offset(-kAutoWithSize(22));
        make.height.mas_equalTo(@(25.0f));
    }];
    
    [self.acountType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.acountName.mas_bottom).offset(kAutoWithSize(7.0f));
        make.left.equalTo(self.acountName);
        make.right.equalTo(self).offset(-kAutoWithSize(22));
    }];
}


#pragma mark - lazyGet

- (UIImageView *)icon {
    if (_icon == nil) {
        _icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mine_chooseStore"]];
        [_icon setViewCornerRadius:30.0];
        _icon.userInteractionEnabled = YES;
    }
    return _icon;
}

- (UILabel *)acountName {
    if (_acountName == nil) {
        _acountName = [[UILabel alloc] init];
        _acountName.font = MZBoldFont(18);
        _acountName.textColor = UIColorFromRGB(0xffffff);
        _acountName.text = @"用户名";
    }
    return _acountName;
}

- (UILabel *)acountType {
    if (_acountType == nil) {
        _acountType = [[UILabel alloc] init];
        _acountType.textAlignment = NSTextAlignmentLeft;
        _acountType.font = MZFont(12);
        _acountType.textColor = UIColorFromRGB(0xffffff);
        _acountType.text = @"用户类型";
    }
    return _acountType;
}

- (UIButton *)signin {
    if (!_signin) {
        _signin = [[UIButton alloc] init];
        [_signin setTitle:@"注册" forState:UIControlStateNormal];
        _signin.titleLabel.font = MZBoldFont(20);
        [_signin addTarget:self action:@selector(signinEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _signin;
}

- (UIButton *)login {
    if (!_login) {
        _login = [[UIButton alloc] init];
        [_login setTitle:@"登录" forState:UIControlStateNormal];
        _login.titleLabel.font = MZBoldFont(20);
        [_login addTarget:self action:@selector(liginEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _login;
}

@end
